package com.meishij.fragment;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.meishij.R;
import com.meishij.adapter.ShiHua_Banner_VP_Adapter;
import com.meishij.adapter.ShiHua_Column_Adpater;
import com.meishij.entry.ShihuaEntry;
import com.meishij.server.ShiHuaServer;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShiHuaFragment extends Fragment implements Callback<ShihuaEntry>, ViewPager.OnPageChangeListener {


    private ViewPager banner_viewPager;
    private RecyclerView column_layout;
    private ShiHuaServer shiHuaServer;
    private ShihuaEntry shihuaEntry;
    private ShiHua_Column_Adpater shiHua_column_adpater;
    private ShiHua_Banner_VP_Adapter shiHua_banner_vp_adapter;
    private LinearLayout point_layout;
    private int point_layoutHeight;
    private ImageView point_view;
    private ImageView tranx_view;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    int currentItem = banner_viewPager.getCurrentItem();
                    banner_viewPager.setCurrentItem(++currentItem);
                    handler.sendEmptyMessageDelayed(0, 5000);
                    break;
            }
           super.handleMessage(msg);
        }
    };

    public ShiHuaFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_shihua, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        banner_viewPager = ((ViewPager) view.findViewById(R.id.viewpager_shihua_banner));
        column_layout = ((RecyclerView) view.findViewById(R.id.recyler_shihua_column));
        point_layout = ((LinearLayout) view.findViewById(R.id.linear_shihua_point));
        tranx_view = (ImageView) view.findViewById(R.id.view_vp_shihua_transx);
        column_layout.setLayoutManager(new GridLayoutManager(getContext(), 5));
        banner_viewPager.setOnPageChangeListener(this);
        Retrofit build = new Retrofit.Builder().baseUrl("http://api.meishi.cc/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        shiHuaServer = build.create(ShiHuaServer.class);
        Call<ShihuaEntry> shihuaEntry = shiHuaServer.getShihuaEntry("time");
        //异步调用
        shihuaEntry.enqueue(this);
    }

    //成功后调用
    @Override
    public void onResponse(Call<ShihuaEntry> call, Response<ShihuaEntry> response) {
        shihuaEntry = response.body();
        //设置banner
        shiHua_banner_vp_adapter = new ShiHua_Banner_VP_Adapter(getContext(), shihuaEntry.getActivities2());
        banner_viewPager.setAdapter(shiHua_banner_vp_adapter);
        banner_viewPager.setCurrentItem(1);
        //设置小白点
        if (point_layout.getChildCount() == 0) {
            setWhitePointsLayout();
        }
        //设置column
        shiHua_column_adpater = new ShiHua_Column_Adpater(getContext(), shihuaEntry.getColumn());
        column_layout.setAdapter(shiHua_column_adpater);
    }

    private void setWhitePointsLayout() {
        for (int i = 0; i < shihuaEntry.getActivities2().size(); i++) {
            point_view = new ImageView(getContext());
            point_view.setImageResource(R.drawable.shihua_vp_point_shape_unchecked);
            int point_padding = (int) getContext().getResources().getDimension(R.dimen.padding_point);
            point_view.setPadding(point_padding, 0, point_padding, 0);
            point_layout.addView(point_view);
        }
        point_layout.post(new Runnable() {
            @Override
            public void run() {
                tranx_view.setVisibility(View.VISIBLE);
                ViewCompat.setTranslationX(tranx_view, point_layout.getLeft() + point_view.getPaddingLeft());
            }
        });
        handler.sendEmptyMessageDelayed(0, 5000);
    }

    @Override
    public void onFailure(Call<ShihuaEntry> call, Throwable t) {
        Toast.makeText(getContext(), "吃的太撑了 稍后再试试吧", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        float value = point_layout.getLeft() + (position - 1 + positionOffset) * point_view.getWidth() + point_view.getPaddingLeft();
        value = Math.max(value, point_layout.getLeft());
        value = Math.min(value, point_layout.getRight() - point_view.getPaddingRight());
        ViewCompat.setTranslationX(tranx_view, value);
    }

    @Override
    public void onPageSelected(int position) {
        PagerAdapter adapter = banner_viewPager.getAdapter();
        if (position == 0) {
            banner_viewPager.setCurrentItem(adapter.getCount() - 2, false);
        } else if (position == adapter.getCount() - 1) {
            banner_viewPager.setCurrentItem(1, false);
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
